{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import *\n",
    "from dataset_processed import get_adj_matrix,get_dist_matrix,molgraph_rep\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "import tqdm \n",
    "from rdkit import RDLogger\n",
    "RDLogger.DisableLog('rdApp.*')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>drug_A</th>\n",
       "      <th>drug_B</th>\n",
       "      <th>DDI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>O=C1N(C2CCC(=O)NC2=O)C(=O)C2=CC=CC=C12</td>\n",
       "      <td>OC(CNCC(O)C1CCC2=C(O1)C=CC(F)=C2)C1CCC2=C(O1)C...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[H][C@]12COC(=O)[C@]1([H])[C@H](C1=CC(OC)=C(O)...</td>\n",
       "      <td>COC1=C(C=C2C(OC3=CC(Cl)=C(NC(O)=NC4CC4)C=C3)=C...</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>COC1=CC=C(C=C1)[C@@H]1SC2=C(C=CC=C2)N(CCN(C)C)...</td>\n",
       "      <td>[H][C@@]12CC[C@@](O)(CC=C)[C@@]1(C)CC[C@]1([H]...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[H][C@@]12CC[C@@](O)(C#CC)[C@@]1(C)C[C@H](C1=C...</td>\n",
       "      <td>CCCCC(=O)N(CC1=CC=C(C=C1)C1=CC=CC=C1C1=NNN=N1)...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>C[C@H](NCC1=CC=C(OCC2=CC(F)=CC=C2)C=C1)C(N)=O</td>\n",
       "      <td>CN1C(=O)NC(=O)C(C)(C1=O)C1=CCCCC1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240119</th>\n",
       "      <td>CN1CCN(CC2=CC=C(C=C2)C(=O)NC2=CC(NC3=NC=CC(=N3...</td>\n",
       "      <td>CC(C)(C)NC[C@H](O)C1=CC(CO)=C(O)C=C1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240120</th>\n",
       "      <td>CN(C)CCCN1C2=CC=CC=C2CCC2=CC=CC=C12</td>\n",
       "      <td>CC1=C(C2=NOC(=N2)C2=CC(=C(O)C(O)=C2)[N+]([O-])...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240121</th>\n",
       "      <td>FC1=C(COC2=CC=C3C=C(CC4SC(=O)NC4=O)C=CC3=C2)C=...</td>\n",
       "      <td>COC1=C(OC)C=C(CCN(C)CCCC(C#N)(C(C)C)C2=CC(OC)=...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240122</th>\n",
       "      <td>CCN(CC)C(=S)SSC(=S)N(CC)CC</td>\n",
       "      <td>ClC1=CC2=C(OC(=O)N2)C=C1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240123</th>\n",
       "      <td>ClC1=CC=C(C=C1)C(=O)NCCN1CCOCC1</td>\n",
       "      <td>COC1=C(C=C2C(OC3=CC(Cl)=C(NC(O)=NC4CC4)C=C3)=C...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>240124 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                   drug_A  \\\n",
       "0                  O=C1N(C2CCC(=O)NC2=O)C(=O)C2=CC=CC=C12   \n",
       "1       [H][C@]12COC(=O)[C@]1([H])[C@H](C1=CC(OC)=C(O)...   \n",
       "2       COC1=CC=C(C=C1)[C@@H]1SC2=C(C=CC=C2)N(CCN(C)C)...   \n",
       "3       [H][C@@]12CC[C@@](O)(C#CC)[C@@]1(C)C[C@H](C1=C...   \n",
       "4           C[C@H](NCC1=CC=C(OCC2=CC(F)=CC=C2)C=C1)C(N)=O   \n",
       "...                                                   ...   \n",
       "240119  CN1CCN(CC2=CC=C(C=C2)C(=O)NC2=CC(NC3=NC=CC(=N3...   \n",
       "240120                CN(C)CCCN1C2=CC=CC=C2CCC2=CC=CC=C12   \n",
       "240121  FC1=C(COC2=CC=C3C=C(CC4SC(=O)NC4=O)C=CC3=C2)C=...   \n",
       "240122                         CCN(CC)C(=S)SSC(=S)N(CC)CC   \n",
       "240123                    ClC1=CC=C(C=C1)C(=O)NCCN1CCOCC1   \n",
       "\n",
       "                                                   drug_B  DDI  \n",
       "0       OC(CNCC(O)C1CCC2=C(O1)C=CC(F)=C2)C1CCC2=C(O1)C...    2  \n",
       "1       COC1=C(C=C2C(OC3=CC(Cl)=C(NC(O)=NC4CC4)C=C3)=C...    3  \n",
       "2       [H][C@@]12CC[C@@](O)(CC=C)[C@@]1(C)CC[C@]1([H]...    2  \n",
       "3       CCCCC(=O)N(CC1=CC=C(C=C1)C1=CC=CC=C1C1=NNN=N1)...    2  \n",
       "4                       CN1C(=O)NC(=O)C(C)(C1=O)C1=CCCCC1    2  \n",
       "...                                                   ...  ...  \n",
       "240119               CC(C)(C)NC[C@H](O)C1=CC(CO)=C(O)C=C1    0  \n",
       "240120  CC1=C(C2=NOC(=N2)C2=CC(=C(O)C(O)=C2)[N+]([O-])...    1  \n",
       "240121  COC1=C(OC)C=C(CCN(C)CCCC(C#N)(C(C)C)C2=CC(OC)=...    0  \n",
       "240122                           ClC1=CC2=C(OC(=O)N2)C=C1    2  \n",
       "240123  COC1=C(C=C2C(OC3=CC(Cl)=C(NC(O)=NC4CC4)C=C3)=C...    0  \n",
       "\n",
       "[240124 rows x 3 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## load data. drug_A is SMILES list of the victim drug , drug_B is the SMILES list of perpetrator drug and DDI is list labels\n",
    "toy_dataset = pd.read_csv('tr_dataset.csv')\n",
    "toy_dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "## obtain all SMILES in the toy dataset\n",
    "unique_SMILES_in_toy_dataset = []\n",
    "unique_SMILES_in_toy_dataset.extend(toy_dataset.drug_A)\n",
    "unique_SMILES_in_toy_dataset.extend(toy_dataset.drug_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "## obtain all unique SMILES in the toy dataset\n",
    "unique_SMILES_in_toy_dataset = set(unique_SMILES_in_toy_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1404"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique_SMILES_in_toy_dataset.__len__()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "## create a dict to store all information of drugs\n",
    "all_drugs_dict = {i:{'adj_matrix':[],'dist_matrix':[],'nums_list':[],'cliques':[],'edges':[],'single_dict':[]} for i in unique_SMILES_in_toy_dataset} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "## load tokenizer\n",
    "tokenizer = Mol_Tokenizer('token_id.json') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/1404 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1404/1404 [00:17<00:00, 80.21it/s]\n"
     ]
    }
   ],
   "source": [
    "for drug in tqdm.tqdm(unique_SMILES_in_toy_dataset):\n",
    "    # try:\n",
    "    nums_list1, edges1,cliques1= tokenizer.tokenize(drug)\n",
    "    # dist_matrix1 = get_dist_matrix(mol,cliques1,edges1,single_clique_idx)\n",
    "    dist_matrix1 = get_dist_matrix(nums_list1,edges1)\n",
    "    adjoin_matrix1 = get_adj_matrix(nums_list1,edges1)\n",
    "    all_drugs_dict[drug]['adj_matrix']= adjoin_matrix1 \n",
    "    all_drugs_dict[drug]['dist_matrix']= dist_matrix1 \n",
    "    all_drugs_dict[drug]['nums_list']= nums_list1\n",
    "    all_drugs_dict[drug]['edges']= edges1\n",
    "    all_drugs_dict[drug]['cliques'] = cliques1\n",
    "    all_drugs_dict[drug]['single_dict'] = molgraph_rep(drug,cliques1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save('preprocessed_molecular_info.npy',all_drugs_dict) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
